package cookie.lib;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import java.io.File;

/**
 * Created by IntelliJ IDEA.
 * User: Nathaniel
 * Date: 12/10/11
 * Time: 5:22 PM
 */
public class XMLParsing {
    public static Document getDocument(File file){
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        try{
            DocumentBuilder builder = factory.newDocumentBuilder();
            return builder.parse(file);
        } catch (Exception ex) {
            return null;
        }
    }

    public static String getValue(Element element, String tag){
        NodeList node = element.getElementsByTagName(tag);
        if(node != null && node.getLength() > 0){
            Element temp = (Element) node.item(0);
            return temp.getFirstChild().getNodeValue();
        }
        return null;
    }
}
